package com.xhwl.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.logistics.bo.input.QueryVehicleCarBO;
import com.xhwl.logistics.bo.manipulate.DeleteVehicleCarBO;
import com.xhwl.logistics.dao.mapper.IVehicleCarMapper;
import com.xhwl.logistics.entity.VehicleCar;
import com.xhwl.logistics.po.VehicleCarPO;
import com.xhwl.logistics.service.mybatis.IVehicleCarCustomSqlService;

import java.util.Date;
import java.util.List;
import java.util.Optional;

//@Service
public class VehicleCarServiceImpl extends ServiceImpl<IVehicleCarMapper, VehicleCar> implements IVehicleCarCustomSqlService {

    @Override
    public IPage<VehicleCarPO> queryCars(IPage<?> page, QueryVehicleCarBO queryVehicleCarBO) {
        return getBaseMapper().queryCars(page, queryVehicleCarBO);
    }

    @Override
    public Optional<VehicleCarPO> getCar(Integer id) {
        return getBaseMapper().getCar(id);
    }

    public Wrapper<VehicleCar> conditionForQueryCars(Integer projectId) {
        LambdaUpdateWrapper<VehicleCar> updateWrapper = Wrappers.lambdaUpdate();
        return updateWrapper
                .eq(VehicleCar::getProjectId, projectId);
    }

    public Wrapper<VehicleCar> conditionFotQueryAliveCars(Integer projectId){
        LambdaUpdateWrapper<VehicleCar> updateWrapper = Wrappers.lambdaUpdate();
        return updateWrapper
                .eq(VehicleCar::getProjectId, projectId)
                .eq(VehicleCar::getDeleted,0);
    }

    private Wrapper<VehicleCar> conditionForDeleteCar(DeleteVehicleCarBO deleteVehicleCarBO) {
        LambdaUpdateWrapper<VehicleCar> updateWrapper = Wrappers.lambdaUpdate();
        return updateWrapper
                .eq(VehicleCar::getId, deleteVehicleCarBO.getId())
                .set(VehicleCar::getDeleted, 1)
                .set(VehicleCar::getDeleterAccountId, deleteVehicleCarBO.getDeleterAccountId())
                .set(VehicleCar::getDeleterAccountName, deleteVehicleCarBO.getDeleterAccountName())
                .set(VehicleCar::getUpdatedAt, new Date());
    }

    private Wrapper<VehicleCar> conditionForDeleteCars(List<Integer> idList) {
        LambdaUpdateWrapper<VehicleCar> updateWrapper = Wrappers.lambdaUpdate();
        return updateWrapper
                .in(VehicleCar::getId, idList)
                .set(VehicleCar::getDeleted, 1)
                .set(VehicleCar::getDeleterAccountName, "xxl-job")
                .set(VehicleCar::getUpdatedAt, new Date());
    }

    public void deleteCar(DeleteVehicleCarBO deleteVehicleCarBO) {
        update(conditionForDeleteCar(deleteVehicleCarBO));
    }

    public void deleteCars(List<Integer> idList) {
        update(conditionForDeleteCars(idList));
    }

}
